package com.qiji.video.web.fcontroller;

import com.github.pagehelper.PageInfo;
import com.qiji.video.bean.BrowsingHistory;
import com.qiji.video.bean.Collection;
import com.qiji.video.bean.Users;
import com.qiji.video.bean.dto.UserDto;
import com.qiji.video.exception.ServiceException;
import com.qiji.video.service.IUserService;
import com.qiji.video.util.JwtUtil;
import com.qiji.video.util.Result;
import com.qiji.video.util.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.List;

/**
 * 前台个人中心
 */
@Api( tags = "前台----个人中心")
@RestController
@RequestMapping("/auth/user/info")
public class UserInfoController {
    @Autowired
    private IUserService userService;
    ThreadLocal threadLocal=new ThreadLocal();

    @GetMapping("/getInfoById")
    @ApiOperation("根据id获得用户信息")
    public Result getInfoById(HttpServletRequest request){
        System.out.println(threadLocal.get());
        String userId = JwtUtil.getUserId(request.getHeader("token"));
        return new Result(ResultCode.SELECT_SUCCESS,userService.selectUser(Integer.parseInt(userId)));
    }
    @PostMapping("/updateByPhone")
    @ApiOperation("通过手机号认证修改密码")
    public Result updatePwdById(@RequestBody Users users) throws ServiceException {
        if (userService.updatePwdByPhone(users)>=0){
            return new Result(ResultCode.UPDATE_SUCCESS);
        }
        return new Result(ResultCode.UPDATE_ERROR);
    }
    @PostMapping("/updateByEmail")
    @ApiOperation("通过邮箱认证修改密码")
    public Result updatePwdByEmail(String email,String password,String verCode) throws ServiceException {
        if (userService.updatePwdByEmail(email,password,verCode)>=0){
            return new Result(ResultCode.UPDATE_SUCCESS);
        }
        return new Result(ResultCode.UPDATE_ERROR);
    }

    @GetMapping("/getBrowHistory")
    @ApiOperation("通过用户id获得浏览历史")
    public Result getBrowHistory(HttpServletRequest request,Integer page,Integer size) throws ServiceException {
        String userId = JwtUtil.getUserId(request.getHeader("token"));
        return new Result(ResultCode.SELECT_SUCCESS,userService.selectBhByUid(Integer.parseInt(userId), page, size));
    }
    @GetMapping("/getComments")
    @ApiOperation("通过电影id获得评论")
    public Result getCommentsByVid(Integer vid,Integer page,Integer size) throws ServiceException {
        return new Result(ResultCode.SELECT_SUCCESS,userService.selectByVid(vid,page,size));
    }
    @GetMapping("/getCollect")
    @ApiOperation("通过用户id获得用户收藏电影")
    public Result getCollectById(HttpServletRequest request,Integer page,Integer size) throws ServiceException {
        String userId = JwtUtil.getUserId(request.getHeader("token"));
        return new Result(ResultCode.SELECT_SUCCESS,userService.selectCollectByUid(Integer.parseInt(userId), page, size));
    }
    @PostMapping("/updateInfo")
    @ApiOperation("更新用户信息")
    public Result updateInfo(HttpServletRequest request,@RequestBody UserDto users) throws ServiceException {
        String userId = JwtUtil.getUserId(request.getHeader("token"));
        users.setId(Integer.parseInt(userId));
        userService.updateInfoSelective(users);
        return new Result(ResultCode.UPDATE_SUCCESS);
    }

}
